package com.nulldev.util.concurrency.threadIt.v4.utils.impls;

import java.util.List;

import com.nulldev.util.VariableAPI.ArrayUtils;
import com.nulldev.util.concurrency.threadIt.v4.emapi.extensions.IFutureTaskFlags;
import com.nulldev.util.concurrency.threadIt.v4.emapi.uft.Executable;

public class EMAPIUtils {

	private static final int[] DEFERRED_OPTIMIZATIONS = new int[]
		{ IFutureTaskFlags.FT_HAS_SYNCHRONOUS_DEPENDENCY, IFutureTaskFlags.FT_CONTINIOUS_TASK, };

	public static boolean shouldDeferOptimizations(final Runnable r) {
		if (!(r instanceof Executable<?>))
			return false;

		return (int) ((Executable<?>) r).getFlag(IFutureTaskFlags.FT_HAS_SYNCHRONOUS_DEPENDENCY) == IFutureTaskFlags.FT_TRUE;
	}

	public static boolean shouldDeferOptimizations(final List<Runnable> r) {
		return r.stream()
				/* */
				.filter(entry -> entry instanceof Executable<?>)
				/* */
				.filter(entry -> ArrayUtils.contains(DEFERRED_OPTIMIZATIONS, ((Executable<?>) entry).getFlags()))
				/* */
				.findFirst()
				/* */
				.isPresent();
	}

}
